package com.zl.learning.io.nio;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;

public class UDPClient {
    public static void main(String[] args) {
        try {
            DatagramSocket ds = new DatagramSocket(7777);
            String msg = "HelloWorld";
            DatagramPacket dp = new DatagramPacket(msg.getBytes(), msg.getBytes().length, new InetSocketAddress("localhost", 9999));
            ds.send(dp);
            byte[] buff = new byte[1024];
            DatagramPacket dp2 = new DatagramPacket(buff, buff.length);
            ds.receive(dp2);
            String val = new String(dp2.getData(), StandardCharsets.UTF_8);
            System.out.println(val);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
